class Rogaru::Tilemap::Tile
	
	def self.set_tile_directory(tiledir)
		@@tiledir = tiledir		
	end

	def self.get_tile_directory()
		return @@tiledir		
	end
	
	attr_accessor :walk	
	attr_accessor :swim	
	attr_accessor :jump
	attr_accessor :ledge
	attr_accessor :swim
	attr_accessor :pain
	attr_accessor :up
	attr_accessor :down
	
	def initialize() 
		@images  = []
		@sources = []
		@types   = []
		@max 	 = 0
		@now 	 = 0
		@index   = nil
		# Below are the tile's effects.
		@walk 	 = true		# The tile can be walked over.
		@jump	 = true		# The tile can be jumped over.
		@ledge   = false    # The tile will cause a jump when walked over.
		@swim	 = false	# The tile is some kind of swimmable liquid.
		@pain	 = false	# Walking over the tile causes pain.
		@up	 	 = false    # The tile is a step-up to the next layer.
		@down	 = false    # The tile is a step-down to the previous layer.
		@direction = nil    # The direction of this tile's effect.
	end
	
	def add(surface, source, type)
		@images.push surface
		@sources.push source
		@types.push type
		@max += 1
		return @max - 1
	end
	
	def get_phase(phase) 
		return @images[phase]
	end	
	
	def get()
		return @images[@now]
	end
	
	def advance()
		@now += 1
		@now = 0 if @now >= @max
	end

	def self.xml_get_yesno_property(xml, attribute, default)
		xml_prop  = xml.attributes[attribute]
		if xml_prop.nil? or xml_prop.empty?
			value = default
		elsif xml_prop == 'no'
			value = false
		elsif xml_prop == 'yes'	
			value = true
		else 
			value = default
		end
		return value
	end

	def self.xml_set_yesno_property(value)
		return 'yes' if value
		return 'no'
	end

	def to_xml() 
		xml_tile   = REXML::Element.new 'tile';
		xml_tile.attributes['walk'] 	= Tilemap::Tile.xml_set_yesno_property(self.walk)
		xml_tile.attributes['swim'] 	= Tilemap::Tile.xml_set_yesno_property(self.swim)
		xml_tile.attributes['jump'] 	= Tilemap::Tile.xml_set_yesno_property(self.jump)
		xml_tile.attributes['ledge'] 	= Tilemap::Tile.xml_set_yesno_property(self.ledge)
		xml_tile.attributes['up'] 		= Tilemap::Tile.xml_set_yesno_property(self.up)
		xml_tile.attributes['down'] 	= Tilemap::Tile.xml_set_yesno_property(self.down)
		xml_tile.attributes['pain'] 	= Tilemap::Tile.xml_set_yesno_property(self.pain)
		xml_images = xml_tile.add_element 'images'
		for i in (0...@max)
			xml_image = xml_images.add_element('image')
			xml_image.add_attribute('index', i.to_s)
			xml_image.add_attribute('type' , @types[i].to_s)
			xml_image.add_attribute('src'  , @sources[i].to_s)
		end
		return xml_tile
	end	

	
	def self.new_from_xml(xml_tile)
		tile_dir = Tilemap::Tile.get_tile_directory
		tile = Tilemap::Tile.new()
		tile.walk  = self.xml_get_yesno_property(xml_tile, 'walk' , true )
		tile.swim  = self.xml_get_yesno_property(xml_tile, 'swim' , false)
		tile.jump  = self.xml_get_yesno_property(xml_tile, 'jump' , true )
		tile.ledge = self.xml_get_yesno_property(xml_tile, 'ledge', false)
		tile.up    = self.xml_get_yesno_property(xml_tile, 'up'   , false)
		tile.down  = self.xml_get_yesno_property(xml_tile, 'down' , false)
		tile.pain  = self.xml_get_yesno_property(xml_tile, 'pain' , false)
		xml_tile.elements.each('images/image') do | xml_image |
			imindex = xml_image.attributes['index']
			imtype  = xml_image.attributes['type']
			imsrc   = xml_image.attributes['src']
			filename = File.join(tile_dir ,imsrc)
			case imtype
			when 'solid' 
				image = Image.load_solid(filename)
			when 'colorkey'
				image = Image.load_colorkey(filename)	
			when 'alpha'
				image = Image.load_alpha(filename)	
			else 	
				image = Image.load_solid(filename)
			end	
			tile.add(image, imsrc, imtype)
		end
		
		return tile
	end
	
	def animated?()
		return (@images.size > 1) 
	end
	
end
